
package com.cptravel.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.eventbus.EventBus;
import com.core.eventbus.EventBus.SubscriberChangeListener;
import com.core.util.B5MActivityHelper;
import com.core.util.B5MPreferenceHelper;
import com.core.util.B5MPreferenceKeys;
import com.core.util.B5MToaster;
import com.cptravel.R;
import com.cptravel.api.LoginRequest;
import com.cptravel.api.LoginResponse;
import com.cptravel.tool.EventNames;
import com.cptravel.tool.UserInfoManage;

public class LoginActivity extends B5MBaseActivity implements OnClickListener, SubscriberChangeListener
{
    private EditText et_account, et_password;
    private CheckBox cb_repassword, cb_autologin;
    private Button bt_login, bt_register;
    private LoginRequest mLoginRequest;
    private String userName, pwd;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "登录";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.login;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_repassword = (CheckBox) findViewById(R.id.cb_repassword);
        cb_autologin = (CheckBox) findViewById(R.id.cb_autologin);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_register = (Button) findViewById(R.id.bt_register);
        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        String name = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_USERID);
        String pwd = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_PWD);
        et_account.setText(name);
        et_password.setText(pwd);
        mLoginRequest = new LoginRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                LoginResponse loginResponse = (LoginResponse) b5mBaseResponse;
                JSONObject jsonObject = new JSONObject();
                try
                {
                    jsonObject.put("userid", loginResponse.userid);
                }
                catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                UserInfoManage.getInstance().saveUser(jsonObject.toString());
                B5MPreferenceHelper.saveBoolValue(mContext, B5MPreferenceKeys.REMEMBER_PWD, cb_repassword.isChecked());
                B5MPreferenceHelper.saveBoolValue(mContext, B5MPreferenceKeys.AUTO_LOGIN, cb_autologin.isChecked());
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USER_USERID, mLoginRequest.userid);
                B5MPreferenceHelper.saveStringValue(mContext, B5MPreferenceKeys.USER_PWD, mLoginRequest.pwd);
                EventBus.getInstance().notifiDataUpdate(EventNames.LOGIN_SUCCESS, true);
                finish();
            }
        }, mProgressBar);
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.bt_login:
                toLogin();
                break;
            case R.id.bt_register:
                B5MActivityHelper.startActivity(mActivity, RegisterActivity.class);
                break;
            default:
                break;
        }
    }

    private void toLogin()
    {
        userName = et_account.getText().toString().trim();
        pwd = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(userName))
        {
            toast("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd))
        {
            toast("密码不能为空");
            return;
        }
        mLoginRequest.userid = userName;
        mLoginRequest.pwd = pwd;
        mLoginRequest.start();
    }

    public void toast(String string)
    {
        B5MToaster.showShort(mActivity, string, 0);
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.REGISTER_SUCCESS, this);
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.REGISTER_SUCCESS, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName , Object notificateContent)
    {
        // TODO Auto-generated method stub
        if (EventNames.REGISTER_SUCCESS.equals(notificationName))
        {
            if (notificateContent != null)
            {
                try
                {
                    boolean isRegisterSuccessed = (Boolean) notificateContent;
                    if (isRegisterSuccessed)
                    {
                        String userUame = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_NAME);
                        String pwd = B5MPreferenceHelper.readStringValue(mContext, B5MPreferenceKeys.USER_PWD);
                        et_account.setText(userUame);
                        et_password.setText(pwd);
                    }
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }
            }
        }
    }
}
